package com.google.project.client.ProjectManager;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.project.client.Service.DailyReportDetailService;
import com.google.project.client.Service.DailyReportDetailServiceAsync;
import com.google.project.client.Service.LoginService;
import com.google.project.client.Service.LoginServiceAsync;
import com.google.project.shared.DailyReportDTO;
import com.google.project.shared.DailyReportDetailDTO;
import com.google.project.shared.ProjectTaskDTO;
import com.google.project.shared.UsersDTO;
public class PMEditDailyReportDetail extends DialogBox {

	private static final Binder binder = GWT.create(Binder.class);
	@UiField TextBox txtTaskName;
	@UiField Button btnSave;
	@UiField Button btnClose;
	@UiField Label lblUnit;
	@UiField TextBox txtWorkToFinished;
	@UiField TextArea taContent;

	interface Binder extends UiBinder<Widget, PMEditDailyReportDetail> {
	}

	
	private final DailyReportDetailServiceAsync reportDetailSv = GWT
			.create(DailyReportDetailService.class);
	private final LoginServiceAsync loginService = GWT
	.create(LoginService.class);
	
	DailyReportDetailDTO currReport = new DailyReportDetailDTO();
	DailyReportDTO dailyReportDTO=new DailyReportDTO();
	ProjectTaskDTO subTask = new ProjectTaskDTO();
	List<ProjectTaskDTO> listTask=new ArrayList<ProjectTaskDTO>();
	UsersDTO currUser=new UsersDTO();
	public PMEditDailyReportDetail() {
		setWidget(binder.createAndBindUi(this));
	}
	//contructor
	public PMEditDailyReportDetail(DailyReportDetailDTO dailyReportDetailDTO,DailyReportDTO currDailyReport) {
		setWidget(binder.createAndBindUi(this));
		this.dailyReportDTO=currDailyReport;
		this.currReport = dailyReportDetailDTO;
		txtTaskName.setText(currReport.getTaskName());
		txtWorkToFinished.setText(currReport.getWorkToFinished() + "");
		taContent.setText(currReport.getContent());
		getInfoLogin();
	}
	//getInfoLogin
	public void getInfoLogin(){
		loginService.getCurrentUserDTO(new AsyncCallback<UsersDTO>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(UsersDTO result) {
				currUser=result;
				
			}
		});

				
	}
	@UiHandler("btnSave")
	void onBtnSaveClick(ClickEvent event) {
		Double work = Double.parseDouble(txtWorkToFinished.getText());
		currReport.setUpdateTime(new Date().getTime());
		currReport.setUpdateByUserId(currUser.getUserId());
		reportDetailSv.updateReportDetail(currReport, work, new AsyncCallback<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(Void result) {
				Window.alert("Sửa thành công!");
				PMEntrypoint.get().PMDailyReportDetail(dailyReportDTO);
			}
		});
		this.hide();
	}
	
	@UiHandler("btnClose")
	void onBtnCloseClick(ClickEvent event) {
		this.hide();
	}
}
